<?php ob_start();?>
<html>
<?php
$root = $_SERVER ['DOCUMENT_ROOT'];
// include ($root . '/standart/head.php');
include ($root . '/standart/dateHeader.php');
include ($root . '/standart/checkUser.php');
?>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Start Bootstrap - SB Admin Version 2.0 Demo</title>

<!-- Core CSS - Include with every page -->
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../font-awesome/css/font-awesome.css" rel="stylesheet">

<!-- SB Admin CSS - Include with every page -->
<link href="../css/sb-admin.css" rel="stylesheet">
<script type="text/javascript" src="../js/ajax.js"></script>
<script src="../js/jquery-1.7.2.min.js"></script>
<script src="../js/bootstrap-typeahead.js"></script>
<script>
	$( document ).ready(function() {
	    initScreen();
	    parent.unloadPageX();
	});
</script>
<script type="text/javascript">

	function initScreen(){
		$('#slcFirmAddresses').prop("disabled",true);
	}

	function checkIsFirm(){
		var chxIsFirm = document.getElementById("chxIsFirm");
		if(chxIsFirm.checked){
			$('#slcFirms').val("");
			$('#slcFirms').prop("disabled",true);
			$('#slcFirmAddresses').prop("disabled",true);
			$('#slcFirmAddresses').html("");
			$('#inpSurName').val("");
			$('#inpSurName').prop("disabled",true);
			enableAddressDefinitions();
		}
		else{
			$('#slcFirms').prop("disabled",false);
			$('#inpSurName').prop("disabled",false);
		}
	}
	
	function getFirmInfo(firmId){
		if(firmId != ''){
			$('#slcFirmAddresses').prop("disabled",false);
			url = 'userController.php?CONTENT_TYPE=FIRM_ADDRESS&FIRM_ID='+firmId
		    var htmlVal = ajx(url, '');
		    document.getElementById('slcFirmAddresses').innerHTML = htmlVal;
			enableAddressDefinitions();
		}
		else{
			$('#slcFirmAddresses').prop("disabled",true);
			$('#slcFirmAddresses').html("");
			enableAddressDefinitions();
		}
	}

	function changeFirmAddress(firmAddressId){
		/*-
		bilgileri goruntulenen kullanıcı firma ise, 
			->firma adresi guncellenebilmesi icin adres alanları enable yapilir.
		bilgileri goruntulenen kullanıcı sahis ise, firma adresleri guncellenemez, 
			->bu nedenle adres alanlari disable yapilir
		*/

	    if(document.getElementById("isFirm") != null && document.getElementById("isFirm").innerText == 'Y'){
			//firma - her kosulda enable
	    	enableAddressDefinitions();
		}
	    else{
		    //sahis (veya yeni kullanici tanimi) firma secildiyse disable, secilmediyse yeni adres tanımlanmak isteniyor
	    	if(firmAddressId!=null && firmAddressId!=''){
				disableAddressDefinitions();
			}
			else{
				enableAddressDefinitions();
			}
		}
		
		
	}

	function enableAddressDefinitions(){
		$('#inpAddress1').val("");
		$('#inpAddress1').prop("disabled",false);

		$('#inpAddress2').val("");
		$('#inpAddress2').prop("disabled",false);

		$('#inpCounty').val("");
		$('#inpCounty').prop("disabled",false);

		$('#inpCity').val("");
		$('#inpCity').prop("disabled",false);

		$('#inpPhone').val("");
		$('#inpPhone').prop("disabled",false);

		$('#inpMobilePhone').val("");
		$('#inpMobilePhone').prop("disabled",false);

		$('#inpAddressDefinition').val("");
		$('#inpAddressDefinition').prop("disabled",false);	}
	
	function disableAddressDefinitions(){
		$('#inpAddress1').val("");
		$('#inpAddress1').prop("disabled",true);

		$('#inpAddress2').val("");
		$('#inpAddress2').prop("disabled",true);

		$('#inpCounty').val("");
		$('#inpCounty').prop("disabled",true);

		$('#inpCity').val("");
		$('#inpCity').prop("disabled",true);

		$('#inpPhone').val("");
		$('#inpPhone').prop("disabled",true);

		$('#inpMobilePhone').val("");
		$('#inpMobilePhone').prop("disabled",true);

		$('#inpAddressDefinition').val("");
		$('#inpAddressDefinition').prop("disabled",true);	}
	
</script>

</head>
<body>
	<div id="wrapper">
		<div id="inner-page-wrapper">
			<div class="row">
				<div class="col-lg-12">
					<h1 class="page-header">Kullanıcı Yönetimi</h1>
				</div>
				<!-- /.col-lg-12 -->
			</div>
			<!-- /.row -->
			<div class="row">
				<div class="col-lg-12">
					<div class="panel panel-default">
						<div class="panel-heading">Kullanıcı Arama</div>
						<div class="panel-body">
							<div class="row">
								<div class="col-lg-9">
									<input type="text" class="form-control" id="typeahead" style="width: 75%"
										data-provide="typeahead" data-items="4"
										data-source='[<?php getUsers();?>]'>
								</div>
							</div>
							<!-- /.row (nested) -->
						</div>
						<!-- /.panel-body -->
					</div>
				</div>
				<div class="col-lg-12">
					<div class="panel panel-default">
						<div class="panel-heading">Kullanıcı Bilgileri</div>
						<div class="panel-body">
							<div class="row">
								<div class="col-lg-9">
									<form id="formUserInfo" role="form">
										<div class="form-group">
											<label id="lblName">İsim</label> 
											<input class="form-control"	id="inpUserName" placeholder="İsim" style="width: 75%">
										</div>
										<div class="form-group">
											<label id="lblSurname">Soyisim</label> 
											<input class="form-control" id="inpSurName" placeholder="Soyisim" style="width: 75%" >
										</div>
										<div class="form-group">
											<label id="lblEmail">E-Mail</label> 
											<input class="form-control" id="inpEmail" placeholder="E-Mail" style="width: 75%">
										</div>
										<div class="form-group">
											<label id="lblFirmName">Firma İsmi</label> 
											<select	class="form-control" id="slcFirms" name="slcFirms" onchange="getFirmInfo(this.value)" style="width: 75%">
												<option value="">Seçiniz</option>
                                            	<?php
												$db = connect ();
												$stmt = "SELECT ID, NAME
                                            				FROM GEVEZE_USER
 															WHERE ISFIRM = 'Y' ORDER BY NAME";
												$res = $db->query ( $stmt ) or die ( $stmt . "<br/><br/>" . mysql_error () );
												while ( $row = $res->fetch_assoc () ) {
													echo '<option value="' . $row ['ID'] . '">' . $row ['NAME'] . '</option>';
												}
												$res->close ();
												disconnect($db);
												?>
                                            </select>
										</div>
										<div class="form-group">
											<div class="checkbox">
												<input type="checkbox" id="chxIsFirm" onClick="checkIsFirm()">
												<label for="chxIsFirm" id="lblIsFirm">Firma mı?</label>
											</div>
										</div>
										<div class="form-group">
											<label id="lblFirmAddresses">Firma Adresleri</label> 
											<select id="slcFirmAddresses" class="form-control" onChange="changeFirmAddress(this.value)" disabled style="width: 75%">
											</select>
										</div>
										<div class="form-group">
											<label id="lblAddress1">Adres 1</label>
											<input class="form-control" id="inpAddress1" placeholder="Adres 1">
										</div>
										<div class="form-group">
											<label id="lblAddress2">Adres 2</label>
											<input class="form-control" id="inpAddress2" placeholder="Adres 2">
										</div>
										<div class="form-group">
											<label id="lblCounty">İlçe</label>
											<input class="form-control" id="inpCounty" placeholder="İlçe" style="width: 75%">
										</div>
										<div class="form-group">
											<label id="lblCity">Şehir</label>
											<input class="form-control"	id="inpCity" placeholder="Şehir" style="width: 75%">
										</div>
										<div class="form-group">
											<label id="lblPhone">Telefon</label>
											<input class="form-control" id="inpPhone" placeholder="Telefon" style="width: 75%" >
										</div>
										<div class="form-group">
											<label id="lblMobilePhone">Cep Telefonu</label> 
											<input class="form-control" id="inpMobilePhone"	placeholder="Cep Telefonu" style="width: 75%" >
										</div>
										<div class="form-group">
											<label id="lblAddressDefinition">Adres Tanımı</label>
											<input class="form-control" id="inpAddressDefinition" placeholder="Adres Tanımı" style="width: 75%">
										</div>
									</form>
								</div>
								<!-- /.col-lg-6 -->
							</div>
							<!-- /.row -->
						</div>
						<!-- /.panel-body -->
					</div>
					<!-- /.panel -->
				</div>
				<!-- /.col-lg-12 -->
			</div>
			<!-- /.row -->
		</div>
		<!-- /#page-wrapper -->

	</div>

	<!-- /#wrapper -->
	<div id="invisContentView" style="display: none;"></div>


</body>

</html>
<?php
	function getUsers(){
		$db = connect ();
		$stmt = "SELECT ID, NAME, SURNAME, (SELECT NAME FROM GEVEZE_USER WHERE ID = U.RELATED_FIRM ) AS FIRM_NAME FROM GEVEZE_USER U";
		$res = $db->query ( $stmt ) or die ( $stmt . "<br/><br/>" . mysql_error () );
		$users = "";
		while ( $row = $res->fetch_assoc () ) {
			$users = $users."\"";
			$dash = " - ";
			
			if(!($row ['ID']===NULL)){
				$users = $users.$row ['ID'].$dash;		
			}
		
			if(!($row ['NAME']===NULL)){
				$users = $users.$row ['NAME']." ";
			}
			
			if(!($row ['SURNAME']===NULL)){
				$users = $users.$row ['SURNAME'];
			}
			
			$users = $users.$dash;
			
			if(!($row ['FIRM_NAME']===NULL)){
				$users = $users.$row ['FIRM_NAME'];
			}
			else {
				$users = rtrim($users, $dash);
			}
			$users = $users."\",";
		}
		disconnect($db);
		$users = rtrim($users, ",");
		echo $users;
	}
?>
<?php ob_end_flush(); ?>